using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonPetTips : UIBaseNode
    {
        public enum PetTipBtnType
        {
            Detail = 0,
            Wear,
            TakeOff
        }

        #region Template Generate,don't modify
        protected class UIB_UIN_CommonPetTips
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExArtSpriteNum bgQuality { protected set; get; }
            public UnityEngine.GameObject pet { protected set; get; }
            public UnityEngine.GameObject skll { protected set; get; }
            public UnityEngine.GameObject baseAttribute { protected set; get; }
            public UnityEngine.GameObject pabilityAttribute { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonTakeOff { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonWear { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonDetail { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("bgQuality", out var __tbv0);
                this.bgQuality = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("pet", out var __tbv1);
                this.pet = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skll", out var __tbv2);
                this.skll = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("baseAttribute", out var __tbv3);
                this.baseAttribute = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("pabilityAttribute", out var __tbv4);
                this.pabilityAttribute = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonTakeOff", out var __tbv5);
                this.buttonTakeOff = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonWear", out var __tbv6);
                this.buttonWear = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonDetail", out var __tbv7);
                this.buttonDetail = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_CommonPet petUnit;
        private UIN_HeroSkillCell skill;
        private ListScrollAdapter<UIN_CommonPetTipsBaseAttribute> baseAttrs;
        private ListScrollAdapter<UIN_CommonPetTipsPabilityAttribute> pabilityAttrs;

        private Pet pet;
        private HeroData heroData;
        private List<int> allPabilityAttrCids = ListPool<int>.Get();
        private System.Action<PetTipBtnType, Pet> callOnClickBtn = null;
        #endregion fields

        #region properties

        protected UIB_UIN_CommonPetTips ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonPetTips();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.petUnit = this.InitChildNode<UIN_CommonPet>(this.ui.pet);
            this.skill = this.InitChildNode<UIN_HeroSkillCell>(this.ui.skll);
            this.baseAttrs = this.InitListScroll<UIN_CommonPetTipsBaseAttribute>(this.ui.baseAttribute, this.OnBaseAttrChanged);
            this.pabilityAttrs = this.InitListScroll<UIN_CommonPetTipsPabilityAttribute>(this.ui.pabilityAttribute, this.OnPabilityAttrChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.buttonDetail.onClick.AddListener(this.OnClickDetail);
            this.ui.buttonTakeOff.onClick.AddListener(this.OnClickTakeOff);
            this.ui.buttonWear.onClick.AddListener(this.OnClickWear);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
            if (this.allPabilityAttrCids != null)
            {
                ListPool<int>.Put(this.allPabilityAttrCids);
                this.allPabilityAttrCids = null;
            }
        }

        #region methonds

        private void OnClickDetail()
        {
            this.ExcuteCallOnClick(PetTipBtnType.Detail);
        }

        private void OnClickTakeOff()
        {
            this.ExcuteCallOnClick(PetTipBtnType.TakeOff);
        }

        private void OnClickWear()
        {
            this.ExcuteCallOnClick(PetTipBtnType.Wear);
        }

        private void ExcuteCallOnClick(PetTipBtnType btnType)
        {
            this.callOnClickBtn?.Invoke(btnType, this.pet);
        }

        public void ShowWear(Pet pet, HeroData heroData = null, System.Action<PetTipBtnType, Pet> funcClickBtn = null)
        {
            this.SetData(pet, heroData, funcClickBtn);
            this.HideAllBtns();
            this.Show();
            this.ShowPet();
            this.ShowPetSkill();
            this.ShowPetBaseAttribute();
            this.ShowPetPabilityAttribute();
            this.ShowOperateBtns();
        }

        private void SetData(Pet pet, HeroData heroData, System.Action<PetTipBtnType, Pet> funcClickBtn)
        {
            this.pet = pet;
            this.heroData = heroData;
            this.callOnClickBtn = funcClickBtn;

            this.InitPabilityAttrs();
        }

        private void InitPabilityAttrs()
        {
            this.allPabilityAttrCids.Clear();
            for (int i = 0; i < APIUIPet.PabilityGroupNum; i++)
            {
                var list = this.pet.GetPabilityGroupList(i);
                this.allPabilityAttrCids.AddRange(list);
            }

            if (this.allPabilityAttrCids.Count > 1)
                this.allPabilityAttrCids.Sort(SortComparison);
        }

        private int SortComparison(int cidA, int cidB)
        {
            UIPetSlotType slotTypeA = PetModule.Instance.GetPabilityAttributeSlotType(this.pet, cidA);
            UIPetSlotType slotTypeB = PetModule.Instance.GetPabilityAttributeSlotType(this.pet, cidB);
            if (slotTypeA != slotTypeB)
            {
                return slotTypeA < slotTypeB ? -1 : 1;
            }
            return 0;
        }

        private void ClearData()
        {
            this.pet = null;
            this.callOnClickBtn = null;
            this.allPabilityAttrCids?.Clear();
        }

        private void HideAllBtns()
        {
            this.ui.buttonDetail.SetActive(false);
            this.ui.buttonTakeOff.SetActive(false);
            this.ui.buttonWear.SetActive(false);
        }

        private void ShowPet()
        {
            this.petUnit.Show(this.pet, this.heroData);
        }

        private void ShowPetSkill()
        {
            var skillData = this.pet.skillData;
            var cfgSkill = skillData.cfgData;
            this.skill.Show(skillData);
            this.skill.ShowName(cfgSkill.Name);
        }

        private void ShowPetBaseAttribute()
        {
            var cfg = this.pet.cfgData;
            this.baseAttrs.RefillCells(cfg.AttType.Count);
        }

        private void OnBaseAttrChanged(UIN_CommonPetTipsBaseAttribute item, int index)
        {
            var cfg = this.pet.cfgData;
            int cid = cfg.AttType[index];
            int val = cfg.AttValue[index];
            var cfgAttribute = TableCenter.attribute.Get(cid);
            string strVal = APIAttributeUIUtils.GetStrAttributeValue(cfgAttribute.ShowType, val);
            item.Show(cfgAttribute.Icon_fullPath, cfgAttribute.Name, strVal);
        }

        private void ShowPetPabilityAttribute()
        {
            var cnt = this.allPabilityAttrCids.Count;
            this.pabilityAttrs.RefillCells(cnt);
        }

        private void OnPabilityAttrChanged(UIN_CommonPetTipsPabilityAttribute item, int index)
        {
            int cid = this.allPabilityAttrCids[index];
            var cfg = TableCenter.pabilityAttribute.Get(cid);
            int indexAttr = 0;
            int attrCid = cfg.AttType[indexAttr];
            int attrVal = cfg.AttValve[indexAttr];

            var cfgAttribute = TableCenter.attribute.Get(attrCid);
            string strVal = APIAttributeUIUtils.GetStrAttributeValue(cfgAttribute.ShowType, attrVal);
            UIPetSlotType slotType = PetModule.Instance.GetPabilityAttributeSlotType(this.pet, cid);

            item.Show(slotType, cfgAttribute.Icon_fullPath, cfgAttribute.Name, strVal);
            int cntSlotCost = (slotType <= UIPetSlotType.Unlock) ? cfg.PetSlotCost : 0;
            item.ShowSlot(cntSlotCost);
        }

        private void ShowOperateBtns()
        {
            bool isVwBtn = this.callOnClickBtn != null && this.heroData != null;
            if (!isVwBtn)
            {
                return;
            }
            var petWearer = this.pet.currWearer;
            bool isCanWear = petWearer != this.heroData;
            bool isTakeOff = petWearer == this.heroData;

            this.ui.buttonDetail.SetActive(true);
            this.ui.buttonTakeOff.SetActive(isTakeOff);
            this.ui.buttonWear.SetActive(isCanWear);
        }
        #endregion methonds
    }
}
